global parity
global G
global H
global errTable
global n, 
global k
global n_k;

// (15, 7) double-error correcting code, generation polynomial X^8 + X^4 + X^2 + X + 1
n = 15;
k = 7;
n_k = n - k;
pol = [1 1 1 0 1 0 0 0 1]; 
[H, G] = cyclgen(n, pol);

errTable = syndtable(H);

H = H';


function c = linearEncode(m)
  global G;
  c = modulo(m*G,2);
endfunction

function m = linearDecode(c)
  global H, errTable, k;
  len = size(c,1)
  m = zeros(len , k)
  for j = 1:len
    row = c(j, :);
    s = modulo(row * H,2);
    err = errTable(bi2de(fliplr(s))+1, :);
    row = modulo(err + row,2)
    m(j, :) = row(1:k);
  end
endfunction


  msg = round(rand(1,k));
  code = linearEncode(msg);
  rcv_code = fixErrorNumber(code, 2);
  rcv_msg = linearDecode(rcv_code);
  if isempty(find(msg ~= rcv_msg))
    disp('right')
  else
    disp('error')
  end
  


